package lex.mtd.ui.utils;

import java.awt.*;

import javax.swing.JComponent;
import javax.swing.LookAndFeel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class MyProgressBar extends JComponent implements SwingConstants
{
	/**
	 * The default minimum for a progress bar is 0.
	 */
	static final private int defaultMinimum = 0;
	/**
	 * The default maximum for a progress bar is 100.
	 */
	static final private int defaultMaximum = 100;
	private int min = defaultMinimum;
	private int value = defaultMinimum;
	private int max = defaultMaximum;
	// ui
	private int cellLength = 0;
	private int cellSpacing = 0;
	private Color selectionForeground = null;
	private Color selectionBackground = null;

	public MyProgressBar()
	{
		this( defaultMinimum, defaultMaximum );
	}

	public MyProgressBar( int min, int max )
	{
		this.min = min;
		this.value = min;
		this.max = max;
		// ===============
		LookAndFeel.installColorsAndFont( this, "ProgressBar.background", "ProgressBar.foreground",
			"ProgressBar.font" );
		cellLength = UIManager.getInt( "ProgressBar.cellLength" );
		cellSpacing = UIManager.getInt( "ProgressBar.cellSpacing" );
		selectionForeground = UIManager.getColor( "ProgressBar.selectionForeground" );
		selectionBackground = UIManager.getColor( "ProgressBar.selectionBackground" );
	}

	public double getPercentComplete()
	{
		return value / ((double) (max - min));
	}

	public int getValue()
	{
		return value;
	}

	public int getMinimum()
	{
		return min;
	}

	public int getMaximum()
	{
		return max;
	}

	public void setValue( int n )
	{
		this.value = n;
		this.repaint();
	}

	public void setMinimum( int n )
	{
		this.min = n;
		this.repaint();
	}

	public void setMaximum( int n )
	{
		this.max = n;
		this.repaint();
	}

	@Override
	protected void paintComponent( Graphics g )
	{
		this.paintDeterminate( g, this );
	}

	protected void paintDeterminate( Graphics g, JComponent c )
	{
		if( !(g instanceof Graphics2D) )
		{
			return;
		}
		Insets b = getInsets(); // area for border
		int barRectWidth = getWidth() - (b.right + b.left);
		int barRectHeight = getHeight() - (b.top + b.bottom);
		if( barRectWidth <= 0 || barRectHeight <= 0 )
		{
			return;
		}
		// amount of progress to draw
		int amountFull = getAmountFull( b, barRectWidth, barRectHeight );
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor( getForeground() );
		// draw the cells
		if( cellSpacing == 0 && amountFull > 0 )
		{
			// draw one big Rect because there is no space between cells
			g2.setStroke( new BasicStroke( (float) barRectHeight, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_BEVEL ) );
		}
		g2.drawLine( b.left, (barRectHeight / 2) + b.top, amountFull + b.left, (barRectHeight / 2)
				+ b.top );
		// Deal with possible text painting
		// paintString( g, b.left, b.top, barRectWidth, barRectHeight,
		// amountFull, b );
	}

	protected int getAmountFull( Insets b, int width, int height )
	{
		int amountFull = 0;
		if( (max - min) != 0 )
			amountFull = (int) Math.round( width * getPercentComplete() );
		return amountFull;
	}
}
